'use strict';

/**
 * @author jiyanliang|weizhiqiang
 * @class sf.b2c.mall.framework.comm
 * @description comm通信基类
 */
define([
    'zepto',
    'zepto.cookie',
    'can',
    'underscore',
    'md5',
    'store',
    'sf.b2c.mall.framework.comm',
    'sf.b2c.mall.api.user.findbackCsrf',
  ],

  function($, cookie, can, _, md5, store, SFComm, SFfindbackCsrf) {

    var SFHttps = can.Construct.extend({

      /**
       * @override
       * @function sf.b2c.mall.framework.comm.init
       * @description 初始化方法
       */
      init: function() {
        this.checkCsrfToken();
      },

      checkCsrfToken: function() {

        if (typeof $.fn.cookie('3_ct') === 'undefined' || $.fn.cookie('3_ct') === "0" || $.fn.cookie('3_ct') === null) {
          store.remove("csrfToken");
          return false;
        }

        if ($.fn.cookie('3_ct') == 1 && !store.get('csrfToken')) {

          var findbackCsrf = new SFfindbackCsrf({"https": true});

          findbackCsrf.sendRequest(false, 'jsonp')
            .done(function(data) {
              store.set('csrfToken', data.value);
              // window.location.reload();
              return false;
            })
            .fail(function(error) {
              console.error(error);
            });
        }
      }

    });

    return new SFHttps();
  });